/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jtmcraft.game;

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import jtmcraft.game.units.GameUnit;
import jtmcraft.graphics.ImageResourceFactory;
import jtmcraft.graphics.ImageResourceFactory.eImageResource;

/**
 *
 * @author tom
 */
public class GameMap {

    private int tileMaxCountWidth; //widocznosc
    private int tileMaxCountHeight; //widocznosc
    private int tileVisibleCountWidth;
    private int tileVisibleCountHeight;
    private int width;  //w ilosci elementow
    private int height;  //w ilosci elemetow
    private int maxWidth;
    private int maxHeight;
    private int tileWidth;
    private int tileHeight;
    private int[][][] map;
    private BufferedImage mapBuffor;
    private GraphicsConfiguration graphicsConfiguration;
    private int xOffset;
    private int yOffset;

    private int xScroll = 10;
    private int yScroll = 10;
    
    private boolean validate;
    private ArrayList<GameUnit> buildings = new ArrayList<GameUnit>();

    public GameMap(GraphicsConfiguration graphicsConfiguration) {
        this.graphicsConfiguration = graphicsConfiguration;
    }

    public void Init() {
        map = new int[][][]{
                    {{1}   , {1,8} , {1,20}, {1,21}, {0}   , {0}   , {0}   , {0}   , {0}   , {0}    },
                    {{1}   , {1,8} , {1,5} , {1,12}, {0}   , {0}   , {0}   , {0}   , {0}   , {0}   ,},
                    {{1},    {1,16}, {1,17}, {1,16}, {1,21}, {0}   , {0}   , {0}   , {0}   , {0}   ,},
                    {{1,14}, {1,15}, {1}   , {1}   , {1,16}, {1,10}, {1,10}, {1,10}, {1,21}, {0}   ,},
                    {{1,4} , {1,18}, {1,15}, {1}   , {1}   , {1}   , {1}   , {1}   , {1,4} , {0}   ,},
                    {{1,19}, {0}   , {1,5} , {1}   , {1}   , {1}   , {1}   , {1}   , {1,4} , {0}   ,},
                    {{0}   , {0}   , {1,5} , {1}   , {1}   , {1}   , {1}   , {1}   , {1,4} , {0}   ,},
                    {{0}   , {0}   , {1,18}, {1,11}, {1,7} , {1,11}, {1,15}, {1,14}, {1,19}, {0}   ,},
                    {{1,21}, {0}   , {0}   , {0}   , {0}   , {0}   , {1,18}, {1,19}, {0}   , {0}   ,},
                    {{1,19}, {0}   , {0}   , {0}   , {0}   , {0}   , {0}   , {0}   , {0}   , {0}   ,}
                };

        tileMaxCountHeight = map.length;
        tileMaxCountWidth = map[0].length;

        tileVisibleCountWidth = 5;
        tileVisibleCountHeight = 6;


        tileWidth = eImageResource.eEnvironmetEarthTile.getActiveWidthHeight()[0];
        tileHeight = eImageResource.eEnvironmetEarthTile.getActiveWidthHeight()[1];

        width = tileVisibleCountWidth * tileWidth;
        height = tileVisibleCountHeight * tileHeight;

        maxWidth = tileMaxCountWidth * tileWidth;
        maxHeight = tileMaxCountHeight * tileHeight;

        mapBuffor = graphicsConfiguration.createCompatibleImage(width + tileWidth * 2, height + tileHeight * 2);

        xOffset = 0;
        yOffset = 0;

        createInBuffer();
        validate = true;

    }

    public void drawMap(Graphics2D graphics2D) {
        if (!validate) {
            createInBuffer();
        }
        graphics2D.drawImage(mapBuffor, 0, 0, null);
    }

    private void createInBuffer() {
        Graphics2D graphics2D = (Graphics2D) mapBuffor.getGraphics();

        int jp = xOffset / tileWidth;
        int ip = yOffset / tileHeight;

        int titleXOffset = xOffset % tileWidth;
        int titleYOffset = yOffset % tileHeight;


        ImageResourceFactory imageResourceFactory = ImageResourceFactory.getInstance();
        for (int i = 0; i <= tileVisibleCountHeight; ++i) {
            for (int j = 0; j <= tileVisibleCountWidth; ++j) {
                for( int n = 0; n < map[ip + i][jp + j].length; ++n)
                {
                    eImageResource imageResource = eImageResource.values()[eImageResource.eEnvironmetEarthTile.ordinal() + map[ip + i][jp + j][n]];
                    graphics2D.drawImage(imageResourceFactory.getImage(imageResource), j * tileWidth - titleXOffset, i * tileHeight - titleYOffset, null);
                }
            }
        }

        //wkopiowanie budunkow
        for (GameUnit building : buildings) {
            // if (xOffset < building.getX() && xOffset + width > building.getX()
            //     && yOffset < building.getY() && yOffset + height > building.getY()) {
            graphics2D.drawImage(building.getSprite().getImage(),
                    building.getX() - xOffset,
                    building.getY() - yOffset, null);

            if (building.getPlayerColor() != null) {
               int durWidth = (int) ((float) building.getWidth() * ( (float) building.getDurability()  / (float) building.getMaxDurability() )) + 1;

                graphics2D.setColor(building.getPlayerColor());
                graphics2D.drawRect(building.getX() - xOffset,  building.getY() - yOffset - 5, building.getWidth() + 1, 3);
                graphics2D.fillRect(building.getX() - xOffset,  building.getY() - yOffset - 5, durWidth, 3);
            }
            // }
        }
        validate = true;

        System.out.println("xofset: " + xOffset);
    }

    public void addBuilding(GameUnit building) {
        buildings.add(building);
    }

    public int getxOffset() {
        return xOffset;
    }

    public int getyOffset() {
        return yOffset;
    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public void invalidate() {
        validate = false;
    }


    public void scrollUp() {
        int yTmpOffset = yOffset - yScroll;
        if( yTmpOffset <  0 ) yTmpOffset = 0;

        yOffset = yTmpOffset;
        invalidate();
    }

    public void scrollDown() {


        int yTmpOffset = yOffset + yScroll;
        if (yTmpOffset >= (tileMaxCountHeight * tileHeight - height)) {
            yTmpOffset = tileMaxCountHeight * tileHeight - height - 1;
        }

        yOffset = yTmpOffset;
        invalidate();
    }


    public void scrollLeft() {


        int xTmpOffset = xOffset - xScroll;
        if (xTmpOffset < 0) {
            xTmpOffset = 0;
        }



        /*
        if( xTileOffset * tileWidth )

         */

        xOffset = xTmpOffset;
        invalidate();
    }

    public void scrollRight() {


        int xTmpOffset = xOffset + xScroll;
        if (xTmpOffset >= (tileMaxCountWidth * tileWidth - tileVisibleCountWidth * tileWidth)) {
            xTmpOffset = tileMaxCountWidth * tileWidth - tileVisibleCountWidth *  tileWidth - 1;
        }


        xOffset = xTmpOffset;
        invalidate();
    }

    public List< GameUnit > getBuildings()
    {
        return buildings;
    }

    public int getMaxHeight() {
        return maxHeight;
    }

    public int getMaxWidth() {
        return maxWidth;
    }


}
